﻿/* 
* Copyright (c) 2009, SCUT 06级计科2班软件工程第6小组
* All rights reserved. 
*
* 作    者：何伟杰 
* 完成日期：2009年6月30日 
*  
* 摘    要：生成验证码
*
* 备    注：
*
*/

using System;
using System.Drawing;
namespace Utility
{
    //CheckCode类功能：产生一个指定个数字符组成的字符串的验证码，以及一个类型为bitmap的图片对象
    public class CheckCode
    {
        public CheckCode(int width ,int number) //依次为图片的高度、宽度和验证码字符数，高宽单位为像素
        {
            _imageHeight = (Int32)(1.5f*width/number);
            _imageWidth = width;
            _codeLength = number;
            _stringCheckCode = GenerateCheckCode();
            CreateCheckCodeImage(_stringCheckCode);			
        }

        //成员变量
        private Bitmap _imageCheckCode;               //验证码图片
        private int _imageHeight;                     //图片高
        private int _imageWidth;                      //图片宽
        private String _stringCheckCode;              //验证码字符串
        private int _codeLength;
        private const String CODE_SET = "23456789abdefghijmnpqrtuyABDEFGHJMNPQRTUY暧昧不是我的错";

        //成员函数
        public Bitmap GetCodeImage()
        {
            return _imageCheckCode;
        }
        public int GetHeight()
        {
            return _imageHeight;
        }
        public int GetWidth()
        {
            return _imageWidth;
        }
        public string GetCodeString()
        {
            return _stringCheckCode;
        }
        public void Refresh() //刷新图片
        {
            _stringCheckCode = GenerateCheckCode();
            CreateCheckCodeImage(_stringCheckCode);
        }
		
        private String GenerateCheckCode()
        {
            String checkCode = String.Empty;
            System.Random random = new Random();
            Int32 codeLen=CODE_SET.Length;
            for (int i = 0; i < _codeLength; i++)
            {   //随机产生一个整数                             
                Int32 index = random.Next();
                index %= codeLen;
                checkCode += CODE_SET[index];
            }
            _stringCheckCode = checkCode;
            return checkCode;
        }

        //建立一个随机图形               
        private void CreateCheckCodeImage(string checkCode)
        {
            if (checkCode == null || checkCode.Trim() == String.Empty)
                return;
            _imageCheckCode = new System.Drawing.Bitmap(_imageWidth, _imageHeight);
            Graphics g = Graphics.FromImage(_imageCheckCode);
            try
            {
                //生成随机生成器                             
                Random random = new Random();
                //清空图片背景色                             
                g.Clear(Color.White);
                //画图片的背景噪音点                             
                for (int i = 0; i < 60; i++)
                {
                    int x = random.Next(_imageCheckCode.Width);
                    int y = random.Next(_imageCheckCode.Height);
                    _imageCheckCode.SetPixel(x, y, Color.FromArgb(random.Next()));
                }
                //干扰线
                int yy = random.Next(_imageCheckCode.Height);
                for (int  x = random.Next(_imageCheckCode.Width); x> yy; x--)
                {
                    _imageCheckCode.SetPixel(x, yy, Color.Blue);
                }

                //把产生的随机数以字体的形式写入画面                             
                Font font = new System.Drawing.Font("Arial", _imageWidth/_codeLength/1.5f, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
                System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, _imageCheckCode.Width, _imageCheckCode.Height), Color.Blue, Color.DarkRed, 1.2f, true);
                g.DrawString(checkCode, font, brush, 2, 2);
                //画图片的边框线                             
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, _imageCheckCode.Width - 1, _imageCheckCode.Height - 1);
                
            }
            finally
            {}
        }
    }
}